import numpy as np
import matplotlib.pyplot as plt

# 中文显示
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

# 定义函数
def f(x):
    return np.sin(x) / x

# 生成数据点
x_vals = np.linspace(-2, 2, 1000)
x_vals = x_vals[x_vals != 0]  # 排除x=0
y_vals = f(x_vals)

# 计算极限
limit_0 = 1  # 已知lim(x->0) sin(x)/x = 1

# 寻找有界区域
delta = 0.5  # 选择δ=0.5
x_bound = x_vals[(x_vals > -delta) & (x_vals < delta) & (x_vals != 0)]
y_bound = f(x_bound)
M = 1  # 因为|sin(x)/x| ≤ 1对于所有x≠0

plt.figure(figsize=(12, 6))
plt.plot(x_vals, y_vals, 'b-', linewidth=2, label=r'$f(x) = \frac{\sin x}{x}$')
plt.axhline(y=limit_0, color='r', linestyle='--', alpha=0.7, label=f'极限值 L = {limit_0}')
plt.axhline(y=M, color='g', linestyle='--', alpha=0.7, label=f'界 M = {M}')
plt.axhline(y=-M, color='g', linestyle='--', alpha=0.7)
plt.fill_between(x_bound, -M, M, color='yellow', alpha=0.3, label='有界区域')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('极限与有界性的关系')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

print(f"在去心邻域(-{delta}, {delta})\{{0}}内，函数值满足|f(x)| ≤ {M}")